package com.geng.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.geng.exception.DuplicateCodeException;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.ResourceBundle;

public class DruidUtil {
    //使用德鲁伊连接池，实现数据库的连接，以及对数据库的操作
    //1.定义变量
    private static String driveClassName ;
    private static String url;
    private static String userName;
    private static String password;

    //获得连接的名字
    private Connection conn;
    //获得预状态通道名字
    private PreparedStatement pps ;
    //得到的结果集
    protected ResultSet resultSet;
    //返回受影响的条数
    protected int count;
    //创建德鲁伊对象
    private static DruidDataSource dataSource = new DruidDataSource();
    private static String validationQuery;
    //创建静态代码块实现，加载properties文件，初始化德鲁伊连接池
    static {
        ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
        //进行赋值
        driveClassName = bundle.getString("driveName");
        url = bundle.getString("url");
        userName = bundle.getString("username");
        password = bundle.getString("password");
        validationQuery = bundle.getString("validationQuery");


        //设置德鲁伊参数
        dataSource.setDriverClassName(driveClassName);
        dataSource.setUrl(url);
        dataSource.setUsername(userName);
        dataSource.setPassword(password);
        dataSource.setValidationQuery(validationQuery);
    }

    //1.从连接池中获得连接
    private Connection getConn(){
        try {
            conn = dataSource.getConnection();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return conn;
    }
    //2.创建预状态通道
    private PreparedStatement getPps(String sql){

        try {
            pps = getConn().prepareStatement(sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return pps;
    }

    //3.绑定参数
    private void params(List list){
        if(list!=null&&list.size()>0){
            for(int i=0;i<list.size();i++){
                try {
                    pps.setObject(i+1,list.get(i));
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }

    //4.查询结果集
    protected ResultSet query(String sql,List list){
        getPps(sql);
        params(list);
        try {
            resultSet = pps.executeQuery();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return resultSet;
    }

    //5.更新结果集
    protected int update(String sql,List list) throws DuplicateCodeException{
        getPps(sql);
        params(list);
        try {
            count = pps.executeUpdate();
        } catch (SQLException throwables) {
            //throwables.printStackTrace();
            if(throwables.getMessage().endsWith("for key 'express.code'")){
                //是因为取件码重复，而出现了异常
                DuplicateCodeException e2 = new DuplicateCodeException(throwables.getMessage());
                throw e2;
            }else{
                throwables.printStackTrace();
            }
        }
        return count;
    }

    //6.关闭通道
    protected void closeAll(){
        try {
            if(conn!=null){
                conn.close();
            }
            if(pps!=null){
                pps.close();
            }
            if(resultSet!=null){
                resultSet.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
